package pers.wwz.study.tencentcop.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import pers.wwz.study.tencentcop.request.robot.RobotMsgTextRequest;
import pers.wwz.study.tencentcop.request.robot.RobotText;

import java.util.ArrayList;

/**
 * 发送机器人消息控制器
 * 机器人消息经过测试只能在企业微信中查看,普通微信中无法查看
 */
@RestController
@RequestMapping("/robot")
@Slf4j
public class SendRobotMsgController {

    @Autowired
    RestTemplate restTemplate;

    @Value("${tencent.sendRobotMsgUrl}")
    private String sendRobotMsgUrl;


    /**
     * 发送文本消息
     * @param content
     */
    @PostMapping(value = "/sendTextMsg",produces = {"applicatiom/json"})
    public void getAccessToken(String content){
        log.info("发送机器人消息的地址为:{}",sendRobotMsgUrl);
        RobotMsgTextRequest sendRobotMsgTextRequest = new RobotMsgTextRequest();
        sendRobotMsgTextRequest.setMsgtype("text");
        RobotText text = new RobotText();
        text.setContent(content);
        ArrayList<String> mentionedList = new ArrayList<>();
        mentionedList.add("@all");
        text.setMentioned_list(mentionedList);
        sendRobotMsgTextRequest.setText(text);
        String responseJson = restTemplate.postForObject(sendRobotMsgUrl,sendRobotMsgTextRequest,String.class);
        log.info("发送机器人消息返回信息:{}",responseJson);
    }
}
